import 'package:flutter/material.dart';
import 'package:get_network_state/get_network_state.dart';
import 'package:permission_handler/permission_handler.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  var state = "";

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                onPressed: () async {
                  await GetNetworkState.init();
                  Permission.accessNotificationPolicy.request();
                },
                child: const Text("init"),
              ),
              Text(state),
              ElevatedButton(
                onPressed: () async {
                  state = "${await GetNetworkState.getNetWorkState()}";
                  setState(() {});
                },
                child: const Text("getState"),
              ),
              ElevatedButton(
                onPressed: () async {
                  await GetNetworkState.startMonitoring();
                },
                child: const Text("startMonitoring"),
              ),
              ElevatedButton(
                onPressed: () async {
                  await GetNetworkState.stopMonitoring();
                },
                child: const Text("stopMonitoring"),
              ),
              StreamBuilder(
                stream: GetNetworkState.stream,
                builder:
                    (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
                  print("emmmm：变化");
                  return Text("${snapshot.data}");
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}
